﻿using System;
using Hjt212Parser;
using HpSocketIotGateway.Properties;
//using HpSocketIotGateway.Db.Code1st;
//using HpSocketIotGateway.Properties;
using log4net;

namespace HpSocketIotGateway.Facade
{
    public static class BizStatus
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(BizStatus));
        //public static void SetAllOffline()
        //{
        //    var mn = "AllEnterprises";
        //    try
        //    {
        //        using (var gCtx = new GatewayContext())
        //        {

        //            var now = DateTime.Now;
        //            var qn = now.ToString("yyyyMMddHHmmssfff");
        //            received_msg msg = new received_msg
        //            {
        //                received_datetime = now,
        //                cn = "0000",
        //                datatime = now,
        //                is_length_right = true,
        //                mn = mn,
        //                processed_flag = ProcessFlag.NotProcessed.ToString(),
        //                processed_flag_2 = ProcessFlag.NotProcessed.ToString(),
        //                processed_flag_3 = ProcessFlag.NotProcessed.ToString(),
        //                qn = qn,
        //                received_string = Crc.WrapByCrc("QN=" + qn + ";CN=0000;MN=" + mn + ";CP=&&DataTime=" + now.ToString("yyyyMMddHHmmss") + "&&", "monitor")
        //            };
        //            gCtx.received_msg.Add(msg);
        //            gCtx.SaveChanges();

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex.Message);
        //        Log.Error(ex.Source);
        //    }
        //}

        //public static void SetOffline(string mn)
        //{
        //    if (string.IsNullOrEmpty(mn)) return;
        //    try
        //    {
        //        using (var ctx = new GatewayContext())
        //        {
        //            var now = DateTime.Now;
        //            var qn = now.ToString("yyyyMMddHHmmssfff");
        //            received_msg msg = new received_msg
        //            {
        //                received_datetime = now,
        //                cn = "0000",
        //                datatime = now,
        //                is_length_right = true,
        //                mn = mn,
        //                processed_flag = ProcessFlag.NotProcessed.ToString(),
        //                processed_flag_2 = ProcessFlag.ProcessingFinished.ToString(),
        //                processed_flag_3 = ProcessFlag.ProcessingFinished.ToString(),
        //                qn = qn,
        //                received_string = Crc.WrapByCrc("QN=" + qn + ";CN=0000;MN=" + mn + ";CP=&&DataTime=" + now.ToString("yyyyMMddHHmmss") + "&&", "monitor")
        //            };
        //            if (Settings.Default.ProcessorNum == 2)
        //            {
        //                msg.processed_flag_2 = ProcessFlag.NotProcessed.ToString();
        //            }
        //            else if (Settings.Default.ProcessorNum == 3)
        //            {
        //                msg.processed_flag_2 = ProcessFlag.NotProcessed.ToString();
        //                msg.processed_flag_3 = ProcessFlag.NotProcessed.ToString();
        //            }

        //            ctx.received_msg.Add(msg);
        //            ctx.SaveChanges();

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex.Message);
        //        Log.Error(ex.Source);
        //    }

        //}

        public static void SetOffline(string mn)
        {
            if (string.IsNullOrEmpty(mn)) return;
            var now = DateTime.Now;
            var qn = now.ToString("yyyyMMddHHmmssfff");
            if (Settings.Default.IsGenCmdDifferent)
            {
                BufferFacade.EnqueueToMessageQueue(Crc.WrapByCrc("QN=" + qn + ";CN=G000;MN=" + mn + ";CP=&&DataTime=" + now.ToString("yyyyMMddHHmmss") + "&&", "monitor"));
            }
            else
            {
                BufferFacade.EnqueueToMessageQueue(Crc.WrapByCrc("QN=" + qn + ";CN=0000;MN=" + mn + ";CP=&&DataTime=" + now.ToString("yyyyMMddHHmmss") + "&&", "monitor"));

            }
        }

        //public static void SetOnline(string mn)
        //{
        //    if (string.IsNullOrEmpty(mn)) return;
        //    try
        //    {
        //        using (var ctx = new GatewayContext())
        //        {
        //            var now = DateTime.Now;
        //            var qn = now.ToString("yyyyMMddHHmmssfff");
        //            received_msg msg = new received_msg
        //            {
        //                received_datetime = now,
        //                cn = "9999",
        //                datatime = now,
        //                is_length_right = true,
        //                mn = mn,
        //                processed_flag = ProcessFlag.NotProcessed.ToString(),
        //                processed_flag_2 = ProcessFlag.ProcessingFinished.ToString(),
        //                processed_flag_3 = ProcessFlag.ProcessingFinished.ToString(),
        //                qn = qn,
        //                received_string = Crc.WrapByCrc("QN=" + qn + ";CN=9999;MN=" + mn + ";CP=&&DataTime=" + now.ToString("yyyyMMddHHmmss") + "&&", "monitor")
        //            };
        //            if (Settings.Default.ProcessorNum == 2)
        //            {
        //                msg.processed_flag_2 = ProcessFlag.NotProcessed.ToString();
        //            }
        //            else if (Settings.Default.ProcessorNum == 3)
        //            {
        //                msg.processed_flag_2 = ProcessFlag.NotProcessed.ToString();
        //                msg.processed_flag_3 = ProcessFlag.NotProcessed.ToString();
        //            }
        //            ctx.received_msg.Add(msg);
        //            ctx.SaveChanges();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex);
        //    }

        //}
        public static void SetOnline(string mn)
        {
            if (string.IsNullOrEmpty(mn)) return;
            if(!MyTimer.IsToTheTime("SetOnline_"+mn,1,true)) return;
            var now = DateTime.Now;
            var qn = now.ToString("yyyyMMddHHmmssfff");
            if (Settings.Default.IsGenCmdDifferent)
            {
                BufferFacade.EnqueueToMessageQueue(Crc.WrapByCrc(
                    "QN=" + qn + ";CN=G999;MN=" + mn + ";CP=&&DataTime=" + now.ToString("yyyyMMddHHmmss") + "&&",
                    "monitor"));
            }
            else
            {
                BufferFacade.EnqueueToMessageQueue(Crc.WrapByCrc(
                    "QN=" + qn + ";CN=9999;MN=" + mn + ";CP=&&DataTime=" + now.ToString("yyyyMMddHHmmss") + "&&",
                    "monitor"));
            }
        }
    }
}
